package com.sgave.blog.service.Impl;

import com.sgave.blog.mapper.BlogMapper;
import com.sgave.blog.mapper.LabelMapper;
import com.sgave.blog.model.Label;
import com.sgave.blog.service.LabelService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author fly
 * @date 2024/1/10 11:44
 */
@Service
@AllArgsConstructor
public class LabelServiceImpl implements LabelService {

    private LabelMapper labelMapper;
    private BlogMapper blogMapper;

    @Override
    public int addLabel(Label label) {
        int result = labelMapper.addLabel(label);
        return result;
    }

    @Override
    public List<Label> selectAll() {
        return labelMapper.selectAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(Integer id) {
        //判断标签有没有被使用，如被使用不能删除，没有使用可根据id删除
        int useCount = blogMapper.getLabelUseCount(id);
        if (useCount > 0) {
            return 0;
        } else {
            return labelMapper.deleteById(id);
        }
    }
}
